package com.drivers.historyorder.service.impl;
import com.drivers.historyorder.service.HistoryOrderService;
import com.drivers.model.historyorder.*;
import com.drivers.model.historyorder.vo.OperateVo;
import com.drivers.model.historyorder.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.drivers.historyorder.mapper.HistoryOrderMapper;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class HistoryOrderServiceImpl implements HistoryOrderService {
    @Autowired
    private  HistoryOrderMapper historyOrderMapper;

    /**
     * 根据司机ID查询订单
     * 通过司机的ID获取该司机的所有历史订单的基本消息
     * 然后为每个订单根据订单编号查找金钱信息和时间信息
     *
     * @param driverId 司机的ID，用于查询该司机的订单
     * @return 返回一个OrderVo对象列表，包含司机的所有订单详细信息
     */
    @Override
    public List<OrderVo> findOrder(Integer driverId) {

        /*// 查询所有订单基本信息
        List<OrderInformation> orderInformationList = historyOrderMapper.findOrderInformation(driverId);
        // 获取该司机所有的订单号
        List<String> orderIds = orderInformationList.stream()
                .map(OrderInformation::getOrderId)
                .collect(Collectors.toList());
        // 根据订单号查询所有订单费用信息
        Map<String, OrderCost> orderCostMap = historyOrderMapper.findOrderCostByOrderIds(orderIds)
                .stream()
                .collect(Collectors.toMap(OrderCost::getOrderId, cost -> cost));
        // 根据订单号查询所有订单时间信息
        Map<String, OrderTime> orderTimeMap = historyOrderMapper.findOrderTimeByOrderIds(orderIds)
                .stream()
                .collect(Collectors.toMap(OrderTime::getOrderId, time -> time));

        // 创建OrderVo对象列表
        List<OrderVo> orderVoList = orderInformationList.stream()
                .map(orderInformation -> {
                    // 创建OrderVo对象
                    OrderVo orderVo = new OrderVo();
                    // 设置订单基本信息
                    orderVo.setOrderInformation(orderInformation);
                    // 设置订单费用信息
                    orderVo.setOrderCost(orderCostMap.get(orderInformation.getOrderId()));
                    // 设置订单时间信息
                    orderVo.setOrderTime(orderTimeMap.get(orderInformation.getOrderId()));
                    // 返回OrderVo对象
                    return orderVo;
                })
                // 根据订单时间倒序排序
                .sorted((o1, o2) -> o2.getOrderTime().getOrderTime().compareTo(o1.getOrderTime().getOrderTime()))
                .collect(Collectors.toList());
        return orderVoList;*/







        // 根据司机id查询订单基本信息
      List<OrderInformation>  orderInformationList = historyOrderMapper.findOrderInformation(driverId);
      // 创建一个OrderVo对象列表，用于存储订单详细信息
        List<OrderVo> orderVoList = new ArrayList<>();
        // 遍历订单基本信息列表，为每个订单查找金钱信息和时间信息
        for (OrderInformation orderInformation : orderInformationList) {
            // 创建一个OrderVo对象，用于存储订单详细信息
            OrderVo orderVo = new OrderVo();
            // 设置set订单基本信息
            orderVo.setOrderInformation(orderInformation);
            // 查找金钱信息和时间信息
            OrderCost orderCost = historyOrderMapper.findOrderCost(orderInformation.getOrderId());
            if(orderCost!=null){
                orderVo.setOrderCost(orderCost);
            }
            OrderTime orderTime = historyOrderMapper.findOrderTime(orderInformation.getOrderId());
            if(orderTime!=null){
                orderVo.setOrderTime(orderTime);
            }
            // 将OrderVo对象添加到列表中
            orderVoList.add(orderVo);
        }
        //按照订单时间进行降序排序
        orderVoList.sort(((o1, o2) -> o2.getOrderTime().getOrderTime().compareTo(o1.getOrderTime().getOrderTime())));

// 返回订单详细信息列表
        return orderVoList;
    }


    @Override
    public UserPersonal findUser(Integer userId) {
        // 根据用户ID查询用户信息并返回
        return historyOrderMapper.findUser(userId);
    }

    @Override
    public void addFeedBack(DriverFeedBack driverFeedBack) {
        if(historyOrderMapper.findFeedBack(driverFeedBack.getOrderId())!=null){
            // 修改司机反馈信息
            historyOrderMapper.updateFeedBack(driverFeedBack);
        }else{
            // 添加司机反馈信息
            historyOrderMapper.addFeedBack(driverFeedBack);
        }

    }


    @Override
    public void addAppraise(OrderAppraise orderAppraise) {
        // 判断该订单是否已经评价过
        String orderId = orderAppraise.getOrderId();

        if(historyOrderMapper.findOrderAppraiseByOrderId(orderId)!=null){
        //评价过了 修改
            historyOrderMapper.updateAppraise(orderAppraise);
        }else{
            // 没有评价  添加司机评价信息
            historyOrderMapper.addAppraise(orderAppraise);
        }

    }

    @Override
    public OrderAppraise findOrderAppraiseByOrderId(String orderId) {
        // 根据订单编号查询订单评价信息
        return historyOrderMapper.findOrderAppraiseByOrderId(orderId);
    }

    @Override
    public DriverFeedBack findFeedBack(String orderId) {
        // 根据订单编号查询司机反馈信息
        return historyOrderMapper.findFeedBack(orderId);
    }

    @Override
    public OperateVo getmonth() {
        OperateVo operateVo = new OperateVo();
        operateVo.setOperatePassengerCharges(historyOrderMapper.findpass());
        operateVo.setOperateDriverCharges(historyOrderMapper.finddriver());
        return operateVo;
    }

    @Override
    public BigDecimal getorderMoney(OrderVo orderVo) {
        // 获取接单时间
        Date date1 = new Date(String.valueOf(orderVo.getOrderTime().getReceiveTime()));
        //转成毫秒
        long milliseconds1 = date1.getTime ();
        //获取到达时间
        Date date2 = new Date(String.valueOf(orderVo.getOrderTime().getArriveTime()));
        //转成毫秒
        long milliseconds2 = date2.getTime ();
        //计算秒差
        long c=(milliseconds1-milliseconds2)/1000;

        if(c%60!=0){
            c=c/60+1;
        }else{
            c=c/60;
        }
        //获取计算金额设置
        OperateVo operateVo = new OperateVo();
        operateVo.setOperatePassengerCharges(historyOrderMapper.findpass());
        operateVo.setOperateDriverCharges(historyOrderMapper.finddriver());
        BigDecimal totalAmount = new BigDecimal(0);
        //在免费等待时间内
        if(c<=operateVo.getOperatePassengerCharges().getDepartureWaitingTime()){
            //订单里程小于起步公里
            if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage() ) <=operateVo.getOperatePassengerCharges().getStartingKilometer()){

                totalAmount = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
                totalAmount = totalAmount.add(orderVo.getOrderCost().getHighwayFee());
                totalAmount = totalAmount.add(orderVo.getOrderCost().getBridgeFee());
                totalAmount = totalAmount.add(orderVo.getOrderCost().getParkingFee());
                totalAmount = totalAmount.add(orderVo.getOrderCost().getAdditionalFee());
                totalAmount = totalAmount.subtract(orderVo.getOrderCost().getPreferentialMoney());
            }
            //订单里程大于起步公里
            if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())>operateVo.getOperatePassengerCharges().getStartingKilometer()){

                totalAmount = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
                totalAmount = totalAmount.add(new BigDecimal(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())-operateVo.getOperatePassengerCharges().getStartingKilometer()).multiply(operateVo.getOperatePassengerCharges().getMileageFee()));
                totalAmount = totalAmount.add(orderVo.getOrderCost().getHighwayFee());
                totalAmount = totalAmount.add(orderVo.getOrderCost().getBridgeFee());
                totalAmount= totalAmount.add(orderVo.getOrderCost().getParkingFee());
                totalAmount= totalAmount.add(orderVo.getOrderCost().getAdditionalFee());
                totalAmount= totalAmount.subtract(orderVo.getOrderCost().getPreferentialMoney());

            }

        }
        //超出免费等待时间内
        if (c>operateVo.getOperatePassengerCharges().getDepartureWaitingTime()){
            //订单里程小于起步公里
            if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage() )<=operateVo.getOperatePassengerCharges().getStartingKilometer()){

                totalAmount  = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
                totalAmount = totalAmount.add(new BigDecimal(c-operateVo.getOperatePassengerCharges().getDepartureWaitingTime()).multiply(operateVo.getOperatePassengerCharges().getDepartureWaitingPrice()));
                totalAmount= totalAmount.add(orderVo.getOrderCost().getHighwayFee());
                totalAmount = totalAmount.add(orderVo.getOrderCost().getBridgeFee());
                totalAmount = totalAmount.add(orderVo.getOrderCost().getParkingFee());
                totalAmount = totalAmount.add(orderVo.getOrderCost().getAdditionalFee());
                totalAmount = totalAmount.subtract(orderVo.getOrderCost().getPreferentialMoney());

            }
            //订单里程大于起步公里
            if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())>operateVo.getOperatePassengerCharges().getStartingKilometer()){
                totalAmount  = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
                totalAmount  = totalAmount.add(new BigDecimal(c-operateVo.getOperatePassengerCharges().getDepartureWaitingTime()).multiply(operateVo.getOperatePassengerCharges().getDepartureWaitingPrice()));
                totalAmount = totalAmount.add(new BigDecimal(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())-operateVo.getOperatePassengerCharges().getStartingKilometer()).multiply(operateVo.getOperatePassengerCharges().getMileageFee()));
                totalAmount = totalAmount.add(orderVo.getOrderCost().getHighwayFee());
                totalAmount = totalAmount.add(orderVo.getOrderCost().getBridgeFee());
                totalAmount = totalAmount.add(orderVo.getOrderCost().getParkingFee());
                totalAmount = totalAmount.add(orderVo.getOrderCost().getAdditionalFee());
                totalAmount = totalAmount.subtract(orderVo.getOrderCost().getPreferentialMoney());
            }
        }

        return totalAmount;

    }

    @Override
    public BigDecimal getdrivermoney(BigDecimal jg) {
        //获取计算金额设置
        OperateVo operateVo = new OperateVo();
        operateVo.setOperatePassengerCharges(historyOrderMapper.findpass());
        operateVo.setOperateDriverCharges(historyOrderMapper.finddriver());
        BigDecimal totalAmount = new BigDecimal(0);
        if(operateVo.getOperateDriverCharges().getServiceType()==1){
            if(operateVo.getOperateDriverCharges().getManageType()==1){
                totalAmount= jg.subtract(jg.multiply(
                        BigDecimal.valueOf(operateVo.getOperateDriverCharges().getServiceProportion()).divide(BigDecimal.valueOf(100))
                        ))
                        .subtract(jg.multiply(BigDecimal.valueOf(operateVo.getOperateDriverCharges().getManageProportion())).divide(BigDecimal.valueOf(100)))
                        .subtract(operateVo.getOperateDriverCharges().getInsurancePrice());
            }
            if(operateVo.getOperateDriverCharges().getManageType()==2){
                totalAmount= jg.subtract(operateVo.getOperateDriverCharges().getInsurancePrice())
                        .subtract(operateVo.getOperateDriverCharges().getManagePrice())
                        .subtract(jg.multiply(
                                BigDecimal.valueOf(operateVo.getOperateDriverCharges().getServiceProportion()).divide(BigDecimal.valueOf(100))
                        ));
            }
        }
        if(operateVo.getOperateDriverCharges().getServiceType()==2){
            if(operateVo.getOperateDriverCharges().getManageType()==1){
                totalAmount= jg.subtract(operateVo.getOperateDriverCharges().getServicePrice())
                        .subtract(jg.multiply(BigDecimal.valueOf(operateVo.getOperateDriverCharges().getManageProportion()).divide(BigDecimal.valueOf(100))))
                        .subtract(operateVo.getOperateDriverCharges().getInsurancePrice());
            }
            if(operateVo.getOperateDriverCharges().getManageType()==2){
                totalAmount= jg.subtract(operateVo.getOperateDriverCharges().getServicePrice())
                        .subtract(operateVo.getOperateDriverCharges().getManagePrice())
                        .subtract(operateVo.getOperateDriverCharges().getInsurancePrice());
            }
        }
        return totalAmount;
    }

    @Override
    public BigDecimal getmoney(String orderId) {

        OrderVo orderVo = new OrderVo();
        orderVo.setOrderCost(historyOrderMapper.findOrderCost(orderId));
        orderVo.setOrderTime(historyOrderMapper.findOrderTime(orderId));
        orderVo.setOrderInformation(historyOrderMapper.findone(orderId));
        // 获取接单时间
        Date date1 = new Date(String.valueOf(orderVo.getOrderTime().getReceiveTime()));
        //转成毫秒
        long milliseconds1 = date1.getTime ();
        //获取到达时间
        Date date2 = new Date(String.valueOf(orderVo.getOrderTime().getArriveTime()));
        //转成毫秒
        long milliseconds2 = date2.getTime ();
        //计算秒差
        long c=(milliseconds1-milliseconds2)/1000;

        if(c%60!=0){
            c=c/60+1;
        }else{
            c=c/60;
        }
        //获取计算金额设置
        OperateVo operateVo = new OperateVo();
        operateVo.setOperatePassengerCharges(historyOrderMapper.findpass());
        operateVo.setOperateDriverCharges(historyOrderMapper.finddriver());
        BigDecimal totalAmount = new BigDecimal(0);
        //在免费等待时间内
        if(c<=operateVo.getOperatePassengerCharges().getDepartureWaitingTime()){
            //订单里程小于起步公里
            if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage() ) <=operateVo.getOperatePassengerCharges().getStartingKilometer()){
                totalAmount = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
            }
            //订单里程大于起步公里
            if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())>operateVo.getOperatePassengerCharges().getStartingKilometer()){
                totalAmount = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
                totalAmount = totalAmount.add(new BigDecimal(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())-operateVo.getOperatePassengerCharges().getStartingKilometer()).multiply(operateVo.getOperatePassengerCharges().getMileageFee()));
            }

        }
        //超出免费等待时间内
        if (c>operateVo.getOperatePassengerCharges().getDepartureWaitingTime()){
            //订单里程小于起步公里
            if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage() )<=operateVo.getOperatePassengerCharges().getStartingKilometer()){

                totalAmount  = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
                totalAmount = totalAmount.add(new BigDecimal(c-operateVo.getOperatePassengerCharges().getDepartureWaitingTime()).multiply(operateVo.getOperatePassengerCharges().getDepartureWaitingPrice()));
            }
            //订单里程大于起步公里
            if(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())>operateVo.getOperatePassengerCharges().getStartingKilometer()){
                totalAmount  = totalAmount.add(operateVo.getOperatePassengerCharges().getStartingPrice());
                totalAmount  = totalAmount.add(new BigDecimal(c-operateVo.getOperatePassengerCharges().getDepartureWaitingTime()).multiply(operateVo.getOperatePassengerCharges().getDepartureWaitingPrice()));
                totalAmount = totalAmount.add(new BigDecimal(Integer.valueOf(orderVo.getOrderInformation().getOrderMileage())-operateVo.getOperatePassengerCharges().getStartingKilometer()).multiply(operateVo.getOperatePassengerCharges().getMileageFee()));
            }
        }

        return totalAmount;

    }


}
